﻿
$(document).ready(function () {
    //初始化表单验证
    initValidate();

    //保存
    $("#btnsave").click(function (evt) { evt.preventDefault(); save($(this)); });
    //取消
    $("#btncancel").click(function (evt) { parent.closeDialog(); return false; });
    //
    $("#Menuname_zho").change(function () {
        var ret = CNTOHK($(this).val());
        $("#Menuname_eng").val(ret);
    });
});


function initValidate() {
    //表单验证
    $('#inputform').validate({
        rules: {
            Menuid: {
                required: true, maxlength: 50, minlength: 2,
                remote: {
                    url: baseurl + 'Manage/Menu/ExistCheck',
                    type: 'post',
                    data: {
                        id: $("#Id").val(),
                        menuid: $("#Menuid").val()
                    },
                    dataFilter: function (data, type) {
                        if (data == "yes")
                            return true;
                        else
                            return false;
                    }
                }
            },
            Menuname_zho: {
                required: true, maxlength: 50, minlength: 2
            }
        },
        errorPlacement: function (error, element) {
            error.appendTo(element.parent());
        },
        messages: {
            Menuid: {
                remote: '菜单ID已经存在'
            }
        }
        //success: function (label) {
        //    label.html("<span style=\"color:green\"></span>").addClass("success");
        //}
    });
}

function save(obj) {
    if (!$("#inputform").valid()) {
        return;
    }

    var jsondata = {
        Id: $("#Id").val(),
        Values: {}
    };
    //主表数据
    $("#inputform .datafield").each(function () {
        jsondata.Values[$(this).attr("data-fieldname")] = $(this).val();
    });

    ////菜单状态（是否停用）
    //jsondata.Values.Status = $("#inputform [data-fieldname='Status']").prop("checked") ? 1 : 0;

    //禁用保存按钮，防止重复提交
    $(obj).attr("disabled", true);

    $.ajax({
        type: "post",
        url: baseurl + "Manage/Menu/Save",
        data: { json: JSON.stringify(jsondata) },
        success: function (e) {
            $(obj).attr("disabled", false);
            if (e.Ret == "0") {
                parent.closeDialog(true);
            } else {
                $.messager.alert(csms_lang.message_error, e.Msg);
            }
        }
    });
}
